﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Text.RegularExpressions;
using Estimate.Common.Estimate.EstimateValue;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Repository;
using Estimate.Common.Entity;

namespace Estimate.Common.Estimate
{
    public class TempPrediction
    {
         /// <summary>
        /// 预报站对应的实况站的对应字典
        /// </summary>
        protected Dictionary<string, List<string>> _realStation4ForcastStation;

        private string _connectionString;

        private RealStationCompareType _compareType = RealStationCompareType.None;

        public RealStationCompareType CompareType
        {
            get { return _compareType; }
            set
            {
                _compareType = value;
                ResetStationCompare();
            }
        }

        /// <summary>
        /// 以预报站的站号为key，存放预报情况
        /// </summary>
        protected Dictionary<string, TempForecast> _forecastList;

        /// <summary>
        /// 将存储的处理好的预报结果字符串化
        /// 或由字符串初始化 Forecast 类
        /// </summary>
        public string SemiResult
        {
            get
            {
                List<string> semiResult = new List<string>(_forecastList.Count);
                foreach (TempForecast forecast in _forecastList.Values)
                {
                    semiResult.Add(forecast.SemiResult);
                }
                return string.Join("|", semiResult.ToArray());
            }           
        }

        public TempPrediction(string connectionString, RealStationCompareType type)
        {
            _connectionString = connectionString;
            CompareType = type;
            
        }

        private void ResetStationCompare()
        {
            RealStationCompareRepository repository = new RealStationCompareRepository(_connectionString);
            ForeRealStationCompareEntity[] entitys = repository.GetAllRecord(_compareType);

            _realStation4ForcastStation = new Dictionary<string, List<string>>();
            _forecastList = new Dictionary<string, TempForecast>();

            foreach (ForeRealStationCompareEntity entity in entitys)
            {
                if (!_realStation4ForcastStation.ContainsKey(entity.RealStation))
                {
                    _realStation4ForcastStation[entity.RealStation] = new List<string>();
                }
                _realStation4ForcastStation[entity.RealStation].Add(entity.ForeStation);
                TempForecast forecast = new TempForecast();
                forecast.FStNum = entity.ForeStation;
                _forecastList[entity.ForeStation] = forecast;
            }     
        }

        /// <summary>
        /// 输入预报站及对应的预报值
        /// </summary>
        /// <param name="value">站号;预报值| 的字符串</param>
        public void InputPrdiction(string value)
        {
            string pattern = @"\|";
            string[] forserial = Regex.Split(value, pattern, RegexOptions.IgnoreCase);
            for (int i = 0; i < forserial.Length; i++)
            {
                string[] oserial = Regex.Split(forserial[i], @"\;", RegexOptions.IgnoreCase);

                
                float result;
                string fstnum = oserial[0];
                if (_forecastList.ContainsKey(fstnum))
                {
                    _forecastList[fstnum].FStNum = fstnum;
                    float.TryParse(oserial[1].ToString(), out result);
                    _forecastList[fstnum].FValue = result;
                }
            }
        }

        /// <summary>
        /// 输入实况站的值
        /// </summary>
        /// <param name="value">站号;观测值| 的字符串</param>
        public void InputReal(string value)
        {
            string[] realserial = Regex.Split(value, @"\|", RegexOptions.IgnoreCase);

            for (int i = 0; i < realserial.Length; i++)
            {
                string[] oserial = Regex.Split(realserial[i], @"\;", RegexOptions.IgnoreCase);

                string rstnum = oserial[0];
                float result;
                float.TryParse(oserial[1].ToString(), out result);

                if (_realStation4ForcastStation.ContainsKey(rstnum))
                {
                    foreach (string fstnum in _realStation4ForcastStation[rstnum])
                    {
                        if (_forecastList.ContainsKey(fstnum))
                        {
                            _forecastList[fstnum].RStNum = rstnum;
                            if (oserial[1].ToString() == "null")
                            {
                                _forecastList[fstnum].SetLackReal();
                            }
                            else
                            {
                                _forecastList[fstnum].RValue = result;
                                _forecastList[fstnum].CheckOffset();
                            }
                        }
                    }
                }                              
            }
        }
        
    }
}
